package com.lsh.controller;

import com.lsh.constants.MessageConstant;
import com.lsh.entity.PageResult;
import com.lsh.entity.QueryPageBean;
import com.lsh.entity.Result;
import com.lsh.pojo.Setmeal;
import com.lsh.service.SetmealService;
import com.lsh.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping("/upload")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result upload(@RequestParam("imgFile")MultipartFile file){
        //获取文件名
        if (!file.isEmpty()) {
            String filename = file.getOriginalFilename();
            //获取文件后缀名
            String filenameExtension = StringUtils.getFilenameExtension(filename);
            //获取uuid组成唯一文件名
            String fileUUIDName = UUID.randomUUID().toString().replace("-", "").toUpperCase() + "." + filenameExtension;
            //调用七牛云工具类上传
            try {
                QiNiuUtils.uploadViaByte(file.getBytes(),fileUUIDName);
                Map<String,String> map = new HashMap<>();
                map.put("fileName",fileUUIDName);
                map.put("domain",QiNiuUtils.DOMAIN);
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,map);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }

    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result add(@RequestParam("checkgroupIds") Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        setmealService.add(checkgroupIds,setmeal);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")

    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);
    }

    /**
     * 点击编辑回显套餐数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        Setmeal setmeal = setmealService.findById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("setmeal",setmeal);
        map.put("domain",QiNiuUtils.DOMAIN);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
    }

    /**
     * 查询套餐检查组关系
     * @param id
     * @return
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(@RequestParam("id") Integer id){
        List<Integer> list = setmealService.findCheckgroupIdsBySetmealId(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /**
     * 提交修改套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result update(@RequestParam("checkgroupIds") Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        setmealService.update(checkgroupIds,setmeal);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    @DeleteMapping("/deleteById")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_HEALTH_MANAGER')")
    public Result deleteById(@RequestParam("id") Integer id){
        setmealService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
